﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security;

namespace MyFitness.View
{
    public partial class LogonForm : Form
    {
        #region Declared Variables
        private Model.Trainee trainee;
        public EventHandler OnLogin;
        public EventHandler OnNewAccount;
        #endregion      

        public LogonForm()
        {
            InitializeComponent();
            CenterToScreen();
        }

        private void OnLoginClick(object sender, EventArgs e)
        {
            if (OnLogin != null)
                OnLogin(this, EventArgs.Empty);
        }

        /// <summary>
        /// Allows a user to sign up for a New Account.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SignUpLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            TraineeAccountProfileForm apf = new TraineeAccountProfileForm();
            DialogResult result = apf.ShowDialog();

            if (result == DialogResult.OK)
            {
                trainee = apf.Trainee;

                if (OnNewAccount != null)
                    OnNewAccount(trainee, EventArgs.Empty);

                Close();
            }
        }

        #region Accessors
        /// <summary>
        /// Gets the Trainee object associated with the login form.
        /// </summary>
        public Model.Trainee GetTrainee
        {
            get
            {
                return trainee;
            }
        }

        /// <summary>
        /// Returns the Username from the Username Textbox.
        /// </summary>
        public string Username
        {
            get
            {
                return UsernameTextBox.Text;
            }
        }

        /// <summary>
        /// Returns the Password from the Password Textbox.
        /// </summary>
        public SecureString Password
        {
            get
            {
                SecureString s = new SecureString();
                foreach (char c in PasswordTextBox.Text)
                {
                    s.AppendChar(c);
                }

                return s;
            }
        }

        /// <summary>
        /// Gets or sets the Error MessageLabel;
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return ErrorLabel.Text;
            }
            set
            {
                ErrorLabel.Text = value;

                if (value.Length > 0)
                    ErrorLabel.Visible = true;
                else
                    ErrorLabel.Visible = false;
            }
        }
        #endregion
    }
}
